package com.BluetoothTest;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import android.util.Log;

public class BluetoothTest extends Activity {

	static BluetoothAdapter mBtAdapter;
	int REQUEST_ENABLE_BLUETOOTH = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        boolean suportRet = supportConfirm();
        if(suportRet == false){
        	Toast.makeText(this, "Bluetooth ON", Toast.LENGTH_SHORT).show();
        	finish();
        }else{
	        Toast.makeText(this, "Bluetooth OFF", Toast.LENGTH_SHORT).show();

	        boolean btEnabledRet = OnOffConfirm();
	        if(btEnabledRet == false){
	        }
        }
    }

    private boolean supportConfirm(){
        BluetoothAdapter Bt = BluetoothAdapter.getDefaultAdapter();
        if(Bt.equals(null)){

        	return false;
        }

        mBtAdapter = Bt;
        return true;
    }

    private boolean OnOffConfirm(){
    	boolean btEnable = mBtAdapter.isEnabled();
    	if(btEnable == true){
    		Intent intent = new Intent(getApplicationContext(), DeviceListActivity.class);
    		startActivity(intent);
    		return true;
    	}
    	Intent BtOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    	startActivityForResult(BtOn, REQUEST_ENABLE_BLUETOOTH);
    	return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int ResultCode, Intent date){

    	if(requestCode == REQUEST_ENABLE_BLUETOOTH){
    		if(ResultCode == Activity.RESULT_OK){
    			Log("Blutooth ON");
         		Intent intent = new Intent(getApplicationContext(), DeviceListActivity.class);
        		startActivity(intent);
    		}else{
    		}
    	}

    }

    public void Log(String string){
		Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
    }
}